package cn.com.chengmandian.core.web.service;

import cn.com.chengmandian.core.web.properties.OssProperties;
import cn.hutool.core.util.StrUtil;
import com.aliyun.oss.OSS;
import com.aliyun.oss.OSSClientBuilder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.stereotype.Component;

import java.io.File;
import java.io.InputStream;

@ConditionalOnProperty(prefix = "cmd.oss", name = "enable", havingValue = "true")
@Component
public class OssService {
    @Autowired
    private OssProperties ossProperties;

    private OSS createClient() {
        return new OSSClientBuilder().build(ossProperties.getEndpoint(), ossProperties.getAccessKeyId(), ossProperties.getAccessKeySecret());
    }

    /**
     * 上传文件
     * @param key
     * @param file
     * @return
     */
    public String putObject(String key, File file) {
        key = key.replace(File.separator, "/");
        if (key.startsWith("/")) key = key.substring(1);
        OSS ossClient = createClient();
        ossClient.putObject(ossProperties.getBucket(), key, file);
        ossClient.shutdown();
        if(StrUtil.isNotBlank(ossProperties.getCdnDomain())){
            return "https://" + ossProperties.getCdnDomain() + "/" + key;
        }
        return "https://" + ossProperties.getBucket() + "." + ossProperties.getEndpoint() + "/" + key;
    }

    /**
     * 上传文件
     * @param key
     * @param file
     * @return
     */
    public String putObject(String key, InputStream file) {
        key = key.replace(File.separator, "/");
        if (key.startsWith("/")) key = key.substring(1);
        OSS ossClient = createClient();
        ossClient.putObject(ossProperties.getBucket(), key, file);
        ossClient.shutdown();
        if(StrUtil.isNotBlank(ossProperties.getCdnDomain())){
            return "https://" + ossProperties.getCdnDomain() + "/" + key;
        }
        return "https://" + ossProperties.getBucket() + "." + ossProperties.getEndpoint() + "/" + key;
    }

    /**
     * 上传文件
     */
    public String putObject(String bucket, String key, File file) {
        key = key.replace(File.separator, "/");
        if (key.startsWith("/")) key = key.substring(1);
        OSS ossClient = createClient();
        ossClient.putObject(bucket, key, file);
        ossClient.shutdown();
        if(StrUtil.isNotBlank(ossProperties.getCdnDomain())){
            return "https://" + ossProperties.getCdnDomain() + "/" + key;
        }
        return "https://" + bucket + "." + ossProperties.getEndpoint() + "/" + key;
    }

    /**
     * 上传文件
     */
    public String putObject(String bucket, String key, InputStream file) {
        key = key.replace(File.separator, "/");
        if (key.startsWith("/")) key = key.substring(1);
        OSS ossClient = createClient();
        ossClient.putObject(bucket, key, file);
        ossClient.shutdown();
        if(StrUtil.isNotBlank(ossProperties.getCdnDomain())){
            return "https://" + ossProperties.getCdnDomain() + "/" + key;
        }
        return "https://" + bucket + "." + ossProperties.getEndpoint() + "/" + key;
    }
}
